package com.onLineCar.driver.dto;

import com.onLineCar.driver.domain.entity.TDriver;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Data
public class DriverInfoDTO {
    private Boolean flag;
    private String driverName;
    private String driverPhone;
    private Integer carId;


    public List<DriverInfoDTO> convertDriverInfoList(List<TDriver> tDrivers, List<Integer> oldCarIdList){
        ArrayList<DriverInfoDTO> driverInfoList = Lists.newArrayList();
        Map<Integer, TDriver> carIdAndDriverInfoMap = tDrivers.stream().collect(Collectors.toMap(TDriver::getCarId, Function.identity(), (v1, v2) -> v1));
        List<Integer> newCarList = tDrivers.stream().map(TDriver::getCarId).distinct().toList();
        Collection<Integer> intersectionList = CollectionUtils.intersection(newCarList, oldCarIdList);
        oldCarIdList.removeAll(intersectionList);
        carIdAndDriverInfoMap.entrySet().stream().filter(integerTDriverEntry -> intersectionList.contains(integerTDriverEntry.getKey())).forEach(integerTDriverEntry -> {
            DriverInfoDTO driverInfoDTO = new DriverInfoDTO();
            driverInfoDTO.setDriverName(integerTDriverEntry.getValue().getDriverName());
            driverInfoDTO.setDriverPhone(integerTDriverEntry.getValue().getDriverTell());
            driverInfoDTO.setCarId(integerTDriverEntry.getKey());
            driverInfoDTO.setFlag(true);
            driverInfoList.add(driverInfoDTO);
        });
        oldCarIdList.forEach(integer -> {
            DriverInfoDTO driverInfoDTO = new DriverInfoDTO();
            driverInfoDTO.setCarId(integer);
            driverInfoDTO.setFlag(false);
            driverInfoList.add(driverInfoDTO);
        });
        return driverInfoList;
    }
//    public List<DriverInfoDTO> convertDriverInfoList(List<TDriver> tDrivers, List<Integer> oldCarIdList){
//        ArrayList<DriverInfoDTO> driverInfoList = Lists.newArrayList();
//        Map<Integer, TDriver> carIdAndDriverInfoMap = tDrivers.stream().collect(Collectors.toMap(TDriver::getCarId, Function.identity()));
//        List<Integer> newCarList = tDrivers.stream().map(TDriver::getCarId).distinct().toList();
//        Collection<Integer> intersectionList = CollectionUtils.intersection(newCarList, oldCarIdList);
//        oldCarIdList.removeAll(intersectionList);
//        carIdAndDriverInfoMap.entrySet().stream().filter(integerTDriverEntry -> intersectionList.contains(integerTDriverEntry.getKey())).forEach(integerTDriverEntry -> {
//            DriverInfoDTO driverInfoDTO = new DriverInfoDTO();
//            driverInfoDTO.setDriverName(integerTDriverEntry.getValue().getDriverName());
//            driverInfoDTO.setDriverPhone(integerTDriverEntry.getValue().getDriverTell());
//            driverInfoDTO.setCarId(integerTDriverEntry.getKey());
//            driverInfoDTO.setFlag(true);
//            driverInfoList.add(driverInfoDTO);
//        });
//        oldCarIdList.forEach(integer -> {
//            DriverInfoDTO driverInfoDTO = new DriverInfoDTO();
//            driverInfoDTO.setCarId(integer);
//            driverInfoDTO.setFlag(false);
//            driverInfoList.add(driverInfoDTO);
//        });
//        return driverInfoList;
//    }
}
